
from pepe.glib import *

zf = lambda x, y: string.zfill(str(x), y)
srj = lambda x, y: string.rjust(str(x), y)
slj = lambda x, y: string.ljust(str(x), y)
rnl = lambda x: re.sub('\n','',x)

#---------------------------------------------------------
def csv_hshow(inpFile):
    ''' csv_hshow(inpFile)
    '''
    fLineA, fLineB = bt.lineno(inpFile, 1), bt.lineno(inpFile, 2)

    # find out column delimiter
    cd = findcoldelimiter(fLineA)

    print fLineA, fLineB
    fLineA, fLineB = rnl(fLineA), rnl(fLineB)
    iIndex = 0
    fLineBcols = string.split(fLineB, cd)
    print srj(inpFile,15), '\n','-'*80
    for colInx, col in enumerate(string.split(fLineA, cd)):
        iIndex += 1
        print zf(iIndex,3), slj(string.strip(col), 40), \
              slj(string.strip(fLineBcols[colInx]), 40)
    print '\n'*2
#---------------------------------------------------------
def csv_vshow(inFile, cIndex):
    ''' csv_vshow(inFile, cIndex)
    '''
    iIndex = 0
    cd = ''
    for line in bt.lines(inFile):
        # find out column delimiter
        if cd == '': cd = findcoldelimiter(line)

        item = string.split(line, cd)[int(cIndex)]
        iIndex += 1
        print zf(iIndex,3), slj(item,40)
#---------------------------------------------------------

def main():
    '''List csv header horizontally or vertically

    Syntax:     csv_show <data file>
    '''
    '''
    if len(sys.argv) == 3 and sys.argv[2][:1] == 'h':
        csv_hshow(sys.argv[1], sys.argv[2][1:2])
    elif len(sys.argv) == 4 and sys.argv[2][:1] == 'v':
        csv_vshow(sys.argv[1], sys.argv[2][1:2], sys.argv[3])
    '''
    if len(sys.argv) == 2:
        csv_hshow(sys.argv[1])
        #csv_vshow(sys.argv[1], 3)
    else:
        print main.__doc__

if __name__ == "__main__":
    main()
